package com.xiaofengstu.mcams.web.service.impl;

import com.xiaofengstu.mcams.constant.SysConstants;
import com.xiaofengstu.mcams.util.ThreadLocalUtils;
import com.xiaofengstu.mcams.web.entity.TAsset;
import com.xiaofengstu.mcams.web.entity.TUser;
import com.xiaofengstu.mcams.web.entity.Workflow;
import com.xiaofengstu.mcams.web.mapper.WorkflowMapper;
import com.xiaofengstu.mcams.web.service.TAssetService;
import com.xiaofengstu.mcams.web.service.TUserService;
import com.xiaofengstu.mcams.web.service.WorkflowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author fengzeng
 * @since 2022-05-07
 */
@Service
@RequiredArgsConstructor
public class WorkflowServiceImpl extends ServiceImpl<WorkflowMapper, Workflow> implements WorkflowService {

  private final WorkflowMapper workflowMapper;

  private TAssetService assetService;

  private final TUserService userService;

  @Autowired
  @Lazy
  public void setAssetService(TAssetService assetService) {
    this.assetService = assetService;
  }

  @Override
  public Integer discardApply(Workflow workflow) {
    Integer userId = ThreadLocalUtils.get();
    workflow.setCreateBy(userId);
    workflow.setWorkflowType("资产报废");
    workflow.setStatus("一级审批");

    TAsset asset = assetService.getById(workflow.getAssetId());
    asset.setStatus("报废申请中");
    assetService.updateById(asset);


    return workflowMapper.insert(workflow);
  }

  @Override
  public Integer repairApply(Workflow workflow) {
    Integer userId = ThreadLocalUtils.get();
    workflow.setCreateBy(userId);
    workflow.setWorkflowType("资产报修");
    workflow.setStatus("一级审批");

    TAsset asset = assetService.getById(workflow.getAssetId());
    asset.setStatus("报废申请中");
    assetService.updateById(asset);

    return workflowMapper.insert(workflow);
  }

  @Override
  public List<Workflow> defineList() {
    Integer userId = ThreadLocalUtils.get();
    TUser user = userService.getById(userId);
    String workflowStatus = user.getRoleId().equals(SysConstants.SYS_CAMPUS_ADMIN_ID) ? "一级审批" : "二级审批";
    return workflowMapper.defineList(workflowStatus);
  }

  @Override
  public Boolean onApprove(Workflow workflow) {
    Integer userId = ThreadLocalUtils.get();
    workflow.setApproveBy(userId);
    workflow.setStatus("一级审批".equals(workflow.getStatus()) ? "二级审批" : "待入库");

    workflowMapper.updateById(workflow);

    TAsset asset = assetService.getById(workflow.getAssetId());
    asset.setStatus("一级审批".equals(workflow.getStatus()) ? "二级审批" : "待入库");
    asset.setStatusStep(asset.getStatusStep() + 1);

    return assetService.updateById(asset);
  }

  @Override
  public Boolean onReject(Workflow workflow) {
    System.out.println(workflow);
    workflow.setApproveBy(ThreadLocalUtils.get());
    workflow.setStatus("不通过");
    workflowMapper.updateById(workflow);

    TAsset asset = assetService.getById(workflow.getAssetId());
    asset.setStatus("不通过");
    return assetService.updateById(asset);
  }

}
